home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / vbcc / machines / amigappc / libsrc / math / floor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-24  |  1.4 KB  |  61 lines

  1. /* Modified version of s_floor.c for vbcc-PowerPC */
  2.  
  3. /*
  4.  * ====================================================
  5.  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  6.  *
  7.  * Developed at SunSoft, a Sun Microsystems, Inc. business.
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software is freely granted, provided that this notice
  10.  * is preserved.
  11.  * ====================================================
  12.  */
  13.  
  14. static const double huge = 1.0e300;
  15.  
  16.  
  17. double floor(double x)
  18. {
  19.     int i0,i1,j0;
  20.     unsigned i,j;
  21.     i0 = *(int*)&x;
  22.     i1 = *(1+(int*)&x);
  23.     j0 = ((i0>>20)&0x7ff)-0x3ff;
  24.     if(j0<20) {
  25.         if(j0<0) {     /* raise inexact if x != 0 */
  26.         if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  27.             if(i0>=0) {i0=i1=0;} 
  28.             else if(((i0&0x7fffffff)|i1)!=0)
  29.             { i0=0xbff00000;i1=0;}
  30.         }
  31.         } else {
  32.         i = (0x000fffff)>>j0;
  33.         if(((i0&i)|i1)==0) return x; /* x is integral */
  34.         if(huge+x>0.0) {    /* raise inexact flag */
  35.             if(i0<0) i0 += (0x00100000)>>j0;
  36.             i0 &= (~i); i1=0;
  37.         }
  38.         }
  39.     } else if (j0>51) {
  40.         if(j0==0x400) return x+x;    /* inf or NaN */
  41.         else return x;        /* x is integral */
  42.     } else {
  43.         i = ((unsigned)(0xffffffff))>>(j0-20);
  44.         if((i1&i)==0) return x;    /* x is integral */
  45.         if(huge+x>0.0) {         /* raise inexact flag */
  46.         if(i0<0) {
  47.             if(j0==20) i0+=1; 
  48.             else {
  49.             j = i1+(1<<(52-j0));
  50.             if(j<i1) i0 +=1 ;     /* got a carry */
  51.             i1=j;
  52.             }
  53.         }
  54.         i1 &= (~i);
  55.         }
  56.     }
  57.     *(int*)&x = i0;
  58.     *(1+(int*)&x) = i1;
  59.     return x;
  60. }
  61.